<?php

class Model_Menu {

    public function insertMenu($menu) {
        if (isset($menu) && $menu != null) {
            $detail = $menu['menuDetail'];
            $query = sprintf("INSERT INTO `menu`(`menuDetail`) 
                              VALUES (N'%s')", mysql_real_escape_string($detail));
            $db = Model_Connections::getInstance()->getDbConnect();
            $data = $db->query($query);
            $query = "Select Max(id) from menu";
            $id = $db->fetchAll($query);
            if ($id != null)
                return $id[0]['Max(id)'];
            return null;
        }
    }

    public function updateMenuById($id, $menudetail) {
        if (isset($id) && $id != null && isset($menudetail) && $menudetail != null) {
            $id = mysql_real_escape_string($id);
            $menudetail = mysql_real_escape_string($menudetail);
            $sql = "UPDATE `menu` SET `menuDetail` = N'" . $menudetail . "'  WHERE id = '" . $id . "'";
            $db = Model_Connections::getInstance()->getDbConnect();
            $data = $db->query($sql);
            return $data;
        }
        return null;
    }

    public function getMenuAllById($id) {
        $sql = "SELECT * FROM menu where id = " . $id;
        $db = Model_Connections::getInstance()->getDbConnect();
        $data = $db->fetchAll($sql);
        return $data;
    }

    public function deleteMenu($id) {
        if (isset($id) && $id != null) {
            $sql = sprintf("DELETE FROM `menu` WHERE id = '%d' ", mysql_real_escape_string($id));
            $db = Model_Connections::getInstance()->getDbConnect();
            $data = $db->query($sql);
            return $data;
        }
    }

    private function processMenu($input) {

        $eachType = explode(";", $input);
        $menuType = array();

        for ($index = 0; $index < count($eachType); $index++) {  
            $menus = array();
            $typeName = explode(":", $eachType[$index]);
            if ($typeName != null && !empty($typeName)) {
                $name = $typeName[0];
                $inputStringMenu = $typeName[1];
                $eachItem = explode("#", $inputStringMenu);
                $menu = array();
                for ($iter = 0; $iter < count($eachItem); $iter++) {
                    $value = $eachItem[$iter];
                    $result = explode("-", $value);
                    $item = array();
                    if ($result != null) {
                        $item['Name'] = trim($result[0]);
                        $item['Price'] = trim($result[1]);
                        $menu[$iter] = $item;
                    }
                }
                $menus["type"] = $name;
                $menus["data"] = $menu;
            }
            $menuType[$index] = $menus;
        }
        return $menuType;
//         $eachItem = explode("#", $inputStringMenu);
//            $menu = array();
//            for ($index = 0; $index < count($eachItem); $index++) {
//                $value = $eachItem[$index];
//                $result = explode("-", $value);
//                $item = array();
//                if ($result != null) {
//                    $item['Name'] = trim($result[0]);
//                    $item['Price'] = trim($result[1]);
//                    $menu[$index] = $item;
//                }
//            }
//            return $menu;
    }

    public function api_getMenuById($id) {
        if ($id != null) {
            $id = mysql_real_escape_string($id);

            $sql = "Select menuDetail from menu where id = " . $id;

            $db = Model_Connections::getInstance()->getDbConnect();
            $data = $db->fetchAll($sql);
            if ($data != null)
                return $this->processMenu($data[0]['menuDetail']);
            else
                return null;
        }
        return null;
    }

}

?>
